<?php
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\LinkPager;
use yii\widgets\Pjax;

$request = Yii::$app->getRequest();
$key = $request->get('key', null);
?>
	<style>
		img.role {
			width: 60px;
			height: 61px;
			float: left;
		}
		input[name="key"]{padding-left: 35px;background:url(<?=Yii::getAlias('@web/backend/img/common/search.png');?>) no-repeat 6px center;}
	</style>
<div class="row">
	<div class="container lh30">
		<a class="topNav" href="<?= Url::to(['/system/site/index']) ?>"><img src="<?= Yii::getAlias('@web/backend/img/home/home.png') ?>" alt="首页"/>返回首页</a>
	</div>
</div>

<div class="row nav-brand-bg borB">
	<div class="container lh30">
		<ul class="nav nav-tabs">
			<li><a href="<?= Url::to(['index']) ?>">会员档案</a></li>
			<li class="active"><a href="<?= Url::to(['analysis']) ?>">会员分析</a></li>
		</ul>
	</div>
</div>

<div class="container mt20">
	<div class="borB clearfix" style="padding-bottom: 20px;">
		<table class="conType table table-striped pull-left" style="width: 50%;">
			<thead>
			<tr>
				<th>次均消费</th>
				<th>人数</th>
				<th>比例</th>
			</tr>
			</thead>
			<tbody>
			<tr>
				<td>500以下</td>
				<td><?= $datas[0]['count'] ?></td>
				<td><?= $datas[0]['ratio'] ?>% </td>
			</tr>
			<tr>
				<td>500-1000</td>
				<td><?= $datas[1]['count'] ?></td>
				<td><?= $datas[1]['ratio'] ?>%</td>
			</tr>
			<tr>
				<td>1001-1500</td>
				<td><?= $datas[2]['count'] ?></td>
				<td><?= $datas[2]['ratio'] ?>%</td>
			</tr>
			<tr>
				<td>1501-2000</td>
				<td><?= $datas[3]['count'] ?></td>
				<td><?= $datas[3]['ratio'] ?>%</td>
			</tr>
			<tr>
				<td>2001-2500</td>
				<td><?= $datas[4]['count'] ?></td>
				<td><?= $datas[4]['ratio'] ?>%</td>
			</tr>
			<tr>
				<td>2500以上</td>
				<td><?= $datas[5]['count'] ?></td>
				<td><?= $datas[5]['ratio'] ?>%</td>
			</tr>

			</tbody>
		</table>
		<div id="report" class="pull-right" style="height: 384px;width: 50%;"></div>
	</div>

	<?php Pjax::begin() ?>
	<div>
		<div class="mt20 clearfix">
			<form action="<?= Url::to(['analysis']) ?>">
				<input class="lh45 mr20" type="text" name="key" placeholder="搜索你感兴趣的内容..." style="vertical-align: bottom;width: 222px;padding-left: 32px;"/>
				<a  class="btnBg dis text-center lh45 " style=" height: 40px;"><input  type="submit" value="搜索"  style="display: block; width: 100%; background: 0; border:0;"></a>
				<div class="select lh45 pull-right">
					<?= Html::dropDownList('sort', $request->get('sort'), [
						'dft' => '默认排序',
						'consume' => '按累计消费',
						'counts' => '按到店次数',
					], ['onchange' => 'actionSearch(this)']); ?>
				</div>
			</form>
			<div class="mt20">
				<?php if(empty($list)){?>
				<table class="table table-bordered">
					<thead>
					<tr>
						<th>会员</th>
						<th>累计服务消费</th>
						<th>到店次数</th>
						<th>次均消费</th>
					</tr>
					</thead>
					<tbody>
					<tr>
						<td colspan="4" align="center"><img src="<?=Yii::getAlias('@web/backend/img/nodata.gif');?>"></td>
					</tr>
					</tbody>
				</table>
				<?php }else{?>
				<table class="table table-bordered">
					<thead>
					<tr>
						<th>会员</th>
						<th>累计服务消费</th>
						<th>到店次数</th>
						<th>次均消费</th>
					</tr>
					</thead>
					<tbody>
					<?php foreach($list as $member): ?>
					<tr>
						<td class="head_img">
							<a href="<?= Url::to(['basic', 'id' => $member['member_id']]) ?>">
								<img class="role" src="" alt="用户头像">
							    <?= $member['member_name'] ?>
							</a>
						</td>
						<td>￥<?= $member['consume_count'] ?></td>
						<td><?= $member['consume_counts'] ?></td>
						<td>
							<?php
						$avgMoney = $member['consume_counts'] ? ceil($member['consume_count'] / $member['consume_counts']) : 0;
						if ($avgMoney > 2500) {
							echo '2500以上';
							} elseif ($avgMoney > 2000) {
							echo '2001-2500';
							} elseif ($avgMoney > 1500) {
							echo '1501-2000';
							} elseif ($avgMoney > 1000) {
							echo '1001-1500';
							} elseif ($avgMoney > 500) {
							echo '500-1000';
							} else {
							echo '500以下';
							}
							?>
						</td>
					</tr>
					<?php endforeach; ?>

					</tbody>
				</table>
				<?php }?>
			</div>
			<script>
				$('.role').each(function () {
					var $id =$(this).parent().attr('href').substr(26);
					$(this).attr('src','/backend/img/member/ico'+($id%20+1)+'.png');
				});
			</script>
			<div class="page">
				<?= LinkPager::widget([
					'pagination' => $page,
					// 'hideOnSinglePage' => true
				]); ?>
			</div>

		</div>
		<?php Pjax::end() ?>
	</div>
</div>


<?php $this->beginBlock('js_end') ?>

function actionSearch(own) {
	var sort = $(own).val();
	if (<?= $key ? 1 : 0 ?> == 1) {
		location.href = '<?= Url::to(['analysis', 'key' => $key]) ?>' + '&sort=' + sort;
	} else {
		location.href = '<?= Url::to(['analysis']) ?>' + '?sort=' + sort;
	}
}

<?php $this->endBlock() ?>
<?php $this->registerJs($this->blocks['js_end'], $this::POS_END); ?>
<?php $this->registerJsFile("@web/libs/echart/echarts.common.min.js",['position'=>$this::POS_END]);?>
<?php $this->registerJsFile("@web/system/member/analysis.js",['position'=>$this::POS_END]);?>